package top.kaoshanji.p5ch08a;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import top.kaoshanji.p5ch08a.mapper.PhoneMapper;
import top.kaoshanji.p5ch08a.model.Phone;
import top.kaoshanji.p5ch08a.util.SqlSessionUtil;

import java.util.Arrays;
import java.util.List;

/**
 * @author kaoshanji
 * ^_^
 * create time 2025/4/20 15:33
 */
public class PhoneMapperTest {

    /**
     * 代码清单test-p5ch08a-160页a：演示分页功能
     */
    @Test
    public void testQueryListPhone() {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            PhoneMapper phoneMapper = sqlSession.getMapper(PhoneMapper.class);

            // 启用分页功能，每页 6 条数据，查询第 4 页的数据
            PageHelper.startPage(4, 6);
            // 执行分页查询
            List<Phone> phoneList = phoneMapper.queryListPhone();

            for (Phone phone: phoneList) {
                System.out.println(phone);
            }

            // 获取分页详情信息..list：查询的结果...navigatePages：页码数量
            PageInfo<Phone> pageInfo = new PageInfo<>(phoneList, 5);
            System.out.println("数据总条数：" + pageInfo.getTotal());
            System.out.println("总页数：" + pageInfo.getPages());
            System.out.println("每页数据条数：" + pageInfo.getPageSize());
            System.out.println("导航页数量：" + pageInfo.getNavigatePages());
            System.out.println("所有导航页码：" + Arrays.toString(pageInfo.getNavigatepageNums()));
            System.out.println("导航起始页码：" + pageInfo.getNavigateFirstPage());
            System.out.println("导航终止页码：" + pageInfo.getNavigateLastPage());
            System.out.println("当前页码：" + pageInfo.getPageNum());
            System.out.println("当前页是否是第一页：" + pageInfo.isIsFirstPage());
            System.out.println("当前页是否是最后一页：" + pageInfo.isIsLastPage());
            System.out.println("当前页是否有上一页：" + pageInfo.isHasPreviousPage());
            System.out.println("当前页是否有下一页：" + pageInfo.isHasNextPage());
            System.out.println("当前页上一页的页码：" + pageInfo.getPrePage());
            System.out.println("当前页下一页的页码：" + pageInfo.getNextPage());
            System.out.println("当前页数据的条数：" + pageInfo.getSize());
            System.out.println("当前页第一个元素在数据表中的行号：" + pageInfo.getStartRow());
            System.out.println("当前页最后一个元素在数据表中的行号：" + pageInfo.getEndRow());

            // 当前页的数据
            List<Phone> currentPageList = pageInfo.getList();
            System.out.println("当前页的数据：" + currentPageList);

        } finally {
            SqlSessionUtil.closeSqlSession(sqlSession);
        }
    }


}
